package com.xyhuo.leetcode.january;

/**
 * 买卖股票的最佳时机
 * https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/
 *
 * @time 2024/1/12
 */
public class MaxProfit {

    public int maxProfit(int[] prices) {
        int min = prices[0];
        int ans = 0;
        for (int i = 1; i < prices.length; i++) {
            if (prices[i] < min) {
                min = prices[i];
            } else {
                if (prices[i] - min > ans) {
                    ans = prices[i] - min;
                }
            }
        }
        return ans;
    }
}
